# 导入库
import requests
import pprint
import csv
file = open('gp1.csv', mode ='a', encoding='utf-8-sig', newline='')
csv_write = csv.DictWriter(file,fieldnames =['股票代码','股票名称','当前价','涨跌额','涨跌幅','年初至今',
                     '成交量','成交额','换手率','市盈率（TTM）','股息率','市值'] )
csv_write.writeheader()
# 1.确定url地址
for page in range(1,53):
    import time
    time.sleep(5)

    print('========================正在抓取第{}页数据======================='.format(page))
    url = 'https://xueqiu.com/service/v5/stock/screener/quote/list?page={}&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1601340894691'.format(str(page))
    #网址是成交额排名：https://xueqiu.com/hq#exchange=CN&plate=1_3_2&firstName=1&secondName=1_3&type=sha&order=desc&order_by=amount
    # 2.发送网络请求
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}
    response = requests.get(url= url,headers = headers)
    json_data = response.json()
    #pprint.pprint(json_data) # 将json数据经过格式化处理
    # 3.数据解析
    data_list = json_data['data']['list']

    for data in data_list:
        data1 = data['symbol']
        data2 = data['name']
        data3 = data['current']
        data4 = data['chg']
        if data4:
            if float(data4) > 0 :
                data4 = '+' + str(data4)
            else:
                data4 = str(data4)
        data5 = str(data['percent']) + '%'
        data6 = str(data['current_year_percent']) + '%'
        data7 = data['volume']
        data8 = data['amount']
        data9 = str(data['turnover_rate']) + '%'
        data10 = data['pe_ttm']
        data11 = data['dividend_yield']
        if data11:
            data11 = str(data['dividend_yield'] ) + '%'
        else:
            data11 = None
        data12 = data['market_capital']

        print(data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12)
        # 4.将数据保存
        data_dict = {'股票代码':data1,'股票名称':data2,'当前价':data3,'涨跌额':data4,'涨跌幅':data5,'年初至今':data6,
                     '成交量':data7,'成交额':data8,'换手率':data9,'市盈率（TTM）':data10,'股息率':data11,'市值':data12}
        csv_write.writerow(data_dict)
